package increment3;

import increment3.statics.Student;

import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.*;
import java.util.ArrayList;

public class GUIFunc extends JFrame{
    public  void windowClosing() {
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }
    public void sleep(){
        long  k = 1000000000L;
        while(k-- > 0){
        }
    }
    public  void setBackground(JFrame f, ImageIcon background) {
        JLabel label = new JLabel(background);        //把背景图片添加到标签里
        label.setBounds(0, 0, background.getIconWidth(), background.getIconHeight());
        JPanel myPanel = (JPanel) f.getContentPane();        //把我的面板设置为内容面板
        myPanel.setOpaque(false);                    //把我的面板设置为不可视
        myPanel.setLayout(new FlowLayout());
        f.getLayeredPane().setLayout(null);        //把分层面板的布局置空
        f.getLayeredPane().add(label, new Integer(Integer.MIN_VALUE));
    }
    public ArrayList<Student> readObject() throws IOException, ClassNotFoundException {
        ArrayList<Student> lst = new ArrayList<>();
        File file = new File("increment3\\StudentInfor.txt");
        if(file.length() != 0){
            ObjectInputStream ois = new ObjectInputStream(new FileInputStream("increment3\\StudentInfor.txt"));
            Object o = ois.readObject();
            ois.close();
            lst = (ArrayList<Student>) o;
        }
        return lst;
    }
    public void writeObject(ArrayList<Student> lst) throws IOException {
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("increment3\\StudentInfor.txt"));
        oos.writeObject(lst);
        oos.close();
    }
}
